﻿using AutoMapper;
using JTTD.Common.Enum;
using JTTD.DataBase;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.System.Response;
using JTTD.Domain.Dtos.System.Request;
using JTTD.Domain.IService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JTTD.Common.Helper;
using System.Xml.Linq;

namespace JTTD.Domain.Service
{
    /// <summary>
    /// 系统配置服务
    /// </summary>
    public class ConfigService : IConfigService
    {

        private readonly JTTDContext _dbContext;
        private readonly IMapper _mapper;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="mapper"></param>
        public ConfigService(JTTDContext context, IMapper mapper)
        {
            _dbContext = context;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取所有配置
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<ConfigInfoResponseDto>>> GetCofigInfoList()
        {
            var list = await _dbContext.ConfigInfoModels.ToListAsync();
            return new ApiResult<List<ConfigInfoResponseDto>>
            {
                IsSuccess = true,
                Data = _mapper.Map<List<ConfigInfoResponseDto>>(list)??new List<ConfigInfoResponseDto> ()
            };
        }

        /// <summary>
        /// 获取指定配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<ApiResult<ConfigInfoResponseDto>> GetConfig(ConfigTypeEnum key)
        {
            var model = await _dbContext.ConfigInfoModels.FirstOrDefaultAsync(item => item.Key == key);
            return new ApiResult<ConfigInfoResponseDto> { IsSuccess=true,Data = _mapper.Map<ConfigInfoResponseDto>(model)??new ConfigInfoResponseDto() };
        }
        /// <summary>
        /// 设置配置
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> SetConfig(SetConfigRequestDto dto)
        {
            try
            {
                var model = await _dbContext.ConfigInfoModels.FirstOrDefaultAsync(item => item.Key == dto.Key);
                if (model == null)
                {
                    model = _mapper.Map<DataBase.Model.System.ConfigInfoModel>(dto);
                    _dbContext.ConfigInfoModels.Add(model);
                    await _dbContext.SaveChangesAsync();
                }
                else
                {
                    model.Value = dto.Value;
                    model.Remark = dto.Remark;
                    _dbContext.ConfigInfoModels.Update(model);
                    await _dbContext.SaveChangesAsync();
                }
                return new ApiResult<bool> { IsSuccess = true, Message = "操作成功" };
            }
            catch(Exception ex)
            {
                return new ApiResult<bool> { IsSuccess=false,Message="操作失败，"+ex.Message};
            }
           
        }

        /// <summary>
        /// 根据用户角色获取导航菜单
        /// </summary>
        /// <param name="userType"></param>
        /// <returns></returns>
       public  async Task<ApiResult<List<LoadXmlResult<MenuInfoResponseDto>>>> GetMenuList(UserTypeEnum userType)
        {
            XDocument xDocument= XmlHelper.LoadXmlConfig("MenuConfig.xml");
            var list= XmlHelper.GetChildNode<MenuInfoResponseDto>(xDocument.Elements().FirstOrDefault());
            if (list.Any())
            {
               list= list.Where(item=>item.Node.UserType==(int)userType).OrderBy(item=>item.Node.Sort).ToList();
               foreach(var item in list)
                {
                    item.Node.MenuUrl = item.Node.MenuUrl;
                    if(item.ChildNodes.Any())
                    {
                        item.ChildNodes.ForEach(args=>
                        {
                            args.Node.MenuUrl =args.Node.MenuUrl;
                        });
                    }
                }
            }
            else
            {
                list = new List<LoadXmlResult<MenuInfoResponseDto>>();
            }
            return new ApiResult<List<LoadXmlResult<MenuInfoResponseDto>>> { IsSuccess = true, Data = list };
        }

    }
}
